home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / display.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  36KB  |  1,224 lines

  1. /* display.c -- readline redisplay facility. */
  2.  
  3. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the GNU Readline Library, a library for
  6.    reading lines of text with interactive input and history editing.
  7.  
  8.    The GNU Readline Library is free software; you can redistribute it
  9.    and/or modify it under the terms of the GNU General Public License
  10.    as published by the Free Software Foundation; either version 1, or
  11.    (at your option) any later version.
  12.  
  13.    The GNU Readline Library is distributed in the hope that it will be
  14.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    The GNU General Public License is often shipped with GNU software, and
  19.    is generally kept in a file called COPYING or LICENSE.  If you do not
  20.    have a copy of the license, write to the Free Software Foundation,
  21.    675 Mass Ave, Cambridge, MA 02139, USA. */
  22. #define READLINE_LIBRARY
  23.  
  24. #if defined (HAVE_CONFIG_H)
  25. #  include "config.h"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include <sys/types.h>
  30.  
  31. #if defined (HAVE_UNISTD_H)
  32. #  include <unistd.h>
  33. #endif /* HAVE_UNISTD_H */
  34.  
  35. #if defined (HAVE_STDLIB_H)
  36. #  include <stdlib.h>
  37. #else
  38. #  include "ansi_stdlib.h"
  39. #endif /* HAVE_STDLIB_H */
  40.  
  41. #include "posixstat.h"
  42.  
  43. /* System-specific feature definitions and include files. */
  44. #include "rldefs.h"
  45.  
  46. /* Some standard library routines. */
  47. #include "readline.h"
  48. #include "history.h"
  49.  
  50. #if !defined (strchr) && !defined (__STDC__)
  51. extern char *strchr (), *strrchr ();
  52. #endif /* !strchr && !__STDC__ */
  53.  
  54. /* Global and pseudo-global variables and functions
  55.    imported from readline.c. */
  56. extern char *rl_prompt;
  57. extern int readline_echoing_p;
  58. extern char *term_clreol, *term_im, *term_ic,  *term_ei, *term_DC;
  59. /* Termcap variables. */
  60. extern char *term_up, *term_dc, *term_cr, *term_IC;
  61. extern int screenheight, screenwidth, screenchars;
  62. extern int terminal_can_insert, term_xn;
  63.  
  64. extern void _rl_output_some_chars ();
  65. extern int _rl_output_character_function ();
  66.  
  67. extern int _rl_output_meta_chars;
  68. extern int _rl_horizontal_scroll_mode;
  69. extern int _rl_mark_modified_lines;
  70. extern int _rl_prefer_visible_bell;
  71.  
  72. /* Pseudo-global functions (local to the readline library) exported
  73.    by this file. */
  74. void _rl_move_cursor_relative (), _rl_output_some_chars ();
  75. void _rl_move_vert ();
  76.  
  77. static void update_line (), clear_to_eol (), space_to_eol ();
  78. static void delete_chars (), insert_some_chars ();
  79.  
  80. extern char *xmalloc (), *xrealloc ();
  81.  
  82. /* Heuristic used to decide whether it is faster to move from CUR to NEW
  83.    by backing up or outputting a carriage return and moving forward. */
  84. #define CR_FASTER(new, cur) (((new) + 1) < ((cur) - (new)))
  85.  
  86. /* **************************************************************** */
  87. /*                                    */
  88. /*            Display stuff                    */
  89. /*                                    */
  90. /* **************************************************************** */
  91.  
  92. /* This is the stuff that is hard for me.  I never seem to write good
  93.    display routines in C.  Let's see how I do this time. */
  94.  
  95. /* (PWP) Well... Good for a simple line updater, but totally ignores
  96.    the problems of input lines longer than the screen width.
  97.  
  98.    update_line and the code that calls it makes a multiple line,
  99.    automatically wrapping line update.  Careful attention needs
  100.    to be paid to the vertical position variables. */
  101.  
  102. /* Keep two buffers; one which reflects the current contents of the
  103.    screen, and the other to draw what we think the new contents should
  104.    be.  Then compare the buffers, and make whatever changes to the
  105.    screen itself that we should.  Finally, make the buffer that we
  106.    just drew into be the one which reflects the current contents of the
  107.    screen, and place the cursor where it belongs.
  108.  
  109.    Commands that want to can fix the display themselves, and then let
  110.    this function know that the display has been fixed by setting the
  111.    RL_DISPLAY_FIXED variable.  This is good for efficiency. */
  112.  
  113. /* Global variables declared here. */
  114. /* What YOU turn on when you have handled all redisplay yourself. */
  115. int rl_display_fixed = 0;
  116.  
  117. /* The stuff that gets printed out before the actual text of the line.
  118.    This is usually pointing to rl_prompt. */
  119. char *rl_display_prompt = (char *)NULL;
  120.  
  121. /* Pseudo-global variables declared here. */
  122. /* The visible cursor position.  If you print some text, adjust this. */
  123. int _rl_last_c_pos = 0;
  124. int _rl_last_v_pos = 0;
  125.  
  126. /* Number of lines currently on screen minus 1. */
  127. int _rl_vis_botlin = 0;
  128.  
  129. /* Variables used only in this file. */
  130. /* The last left edge of text that was displayed.  This is used when
  131.    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
  132. static int last_lmargin = 0;
  133.  
  134. /* The line display buffers.  One is the line currently displayed on
  135.    the screen.  The other is the line about to be displayed. */
  136. static char *visible_line = (char *)NULL;
  137. static char *invisible_line = (char *)NULL;
  138.  
  139. /* A buffer for `modeline' messages. */
  140. static char msg_buf[128];
  141.  
  142. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  143. static int forced_display = 0;
  144.  
  145. /* Default and initial buffer size.  Can grow. */
  146. static int line_size = 1024;
  147.  
  148. static char *last_prompt_string = (char *)NULL;
  149. static char *local_prompt, *local_prompt_prefix;
  150. static int visible_length, prefix_length;
  151.  
  152. /* The number of invisible characters in the line currently being
  153.    displayed on the screen. */
  154. static int visible_wrap_offset = 0;
  155.  
  156. /* The length (buffer offset) of the first line of the last (possibly
  157.    multi-line) buffer displayed on the screen. */
  158. static int visible_first_line_len = 0;
  159.  
  160. /* Expand the prompt string S and return the number of visible
  161.    characters in *LP, if LP is not null.  This is currently more-or-less
  162.    a placeholder for expansion. */
  163.  
  164. /* Current implementation:
  165.     \001 (^A) start non-visible characters
  166.     \002 (^B) end non-visible characters
  167.    all characters except \001 and \002 (following a \001) are copied to
  168.    the returned string; all characters except those between \001 and
  169.    \002 are assumed to be `visible'. */    
  170.  
  171. static char *
  172. expand_prompt (pmt, lp)
  173.      char *pmt;
  174.      int *lp;
  175. {
  176.   char *r, *ret, *p;
  177.   int l, rl, ignoring;
  178.  
  179.   /* Short-circuit if we can. */
  180.   if (strchr (pmt, RL_PROMPT_START_IGNORE) == 0)
  181.     {
  182.       r = savestring (pmt);
  183.       if (lp)
  184.     *lp = strlen (r);
  185.       return r;
  186.     }
  187.  
  188.   l = strlen (pmt);
  189.   r = ret = xmalloc (l + 1);
  190.   
  191.   for (rl = ignoring = 0, p = pmt; p && *p; p++)
  192.     {
  193.       /* This code strips the invisible character string markers
  194.      RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
  195.       if (*p == RL_PROMPT_START_IGNORE)
  196.     {
  197.       ignoring++;
  198.       continue;
  199.     }
  200.       else if (ignoring && *p == RL_PROMPT_END_IGNORE)
  201.     {
  202.       ignoring = 0;
  203.       continue;
  204.     }
  205.       else
  206.     {
  207.       *r++ = *p;
  208.       if (!ignoring)
  209.         rl++;
  210.     }
  211.     }
  212.  
  213.   *r = '\0';
  214.   if (lp)
  215.     *lp = rl;
  216.   return ret;
  217. }
  218.  
  219. /*
  220.  * Expand the prompt string into the various display components, if
  221.  * necessary.
  222.  *
  223.  * local_prompt = expanded last line of string in rl_display_prompt
  224.  *          (portion after the final newline)
  225.  * local_prompt_prefix = portion before last newline of rl_display_prompt,
  226.  *             expanded via expand_prompt
  227.  * visible_length = number of visible characters in local_prompt
  228.  * prefix_length = number of visible characters in local_prompt_prefix
  229.  *
  230.  * This function is called once per call to readline().  It may also be
  231.  * called arbitrarily to expand the primary prompt.
  232.  *
  233.  * The return value is the number of visible characters on the last line
  234.  * of the (possibly multi-line) prompt.
  235.  */
  236. int
  237. rl_expand_prompt (prompt)
  238.      char *prompt;
  239. {
  240.   char *p, *t;
  241.   int c;
  242.  
  243.   /* Clear out any saved values. */
  244.   if (local_prompt)
  245.     free (local_prompt);
  246.   if (local_prompt_prefix)
  247.     free (local_prompt_prefix);
  248.   local_prompt = local_prompt_prefix = (char *)0;
  249.  
  250.   p = strrchr (prompt, '\n');
  251.   if (!p)
  252.     {
  253.       /* The prompt is only one line. */
  254.       local_prompt = expand_prompt (prompt, &visible_length);
  255.       local_prompt_prefix = (char *)0;
  256.       return (visible_length);
  257.     }
  258.   else
  259.     {
  260.       /* The prompt spans multiple lines. */
  261.       t = ++p;
  262.       local_prompt = expand_prompt (p, &visible_length);
  263.       c = *t; *t = '\0';
  264.       /* The portion of the prompt string up to and including the
  265.      final newline is now null-terminated. */
  266.       local_prompt_prefix = expand_prompt (prompt, &prefix_length);
  267.       *t = c;
  268.       return (prefix_length);
  269.     }
  270. }
  271.  
  272. /* Basic redisplay algorithm. */
  273. void
  274. rl_redisplay ()
  275. {
  276.   register int in, out, c, linenum;
  277.   register char *line = invisible_line;
  278.   int c_pos = 0, inv_botlin = 0, wrap_offset, wrap_column;
  279.   char *prompt_this_line;
  280.  
  281.   if (!readline_echoing_p)
  282.     return;
  283.  
  284.   if (!rl_display_prompt)
  285.     rl_display_prompt = "";
  286.  
  287.   if (!invisible_line)
  288.     {
  289.       visible_line = xmalloc (line_size);
  290.       invisible_line = xmalloc (line_size);
  291.       line = invisible_line;
  292.       for (in = 0; in < line_size; in++)
  293.     {
  294.       visible_line[in] = 0;
  295.       invisible_line[in] = 1;
  296.     }
  297.       rl_on_new_line ();
  298.     }
  299.  
  300.   /* Draw the line into the buffer. */
  301.   c_pos = -1;
  302.  
  303.   /* Mark the line as modified or not.  We only do this for history
  304.      lines. */
  305.   out = 0;
  306.   if (_rl_mark_modified_lines && current_history () && rl_undo_list)
  307.     {
  308.       line[out++] = '*';
  309.       line[out] = '\0';
  310.     }
  311.  
  312.   /* If someone thought that the redisplay was handled, but the currently
  313.      visible line has a different modification state than the one about
  314.      to become visible, then correct the caller's misconception. */
  315.   if (visible_line[0] != invisible_line[0])
  316.     rl_display_fixed = 0;
  317.  
  318.   /* If the prompt to be displayed is the `primary' readline prompt (the
  319.      one passed to readline()), use the values we have already expanded.
  320.      If not, use what's already in rl_display_prompt.  WRAP_OFFSET is the
  321.      number of non-visible characters in the prompt string. */
  322.   if (rl_display_prompt == rl_prompt)
  323.     {
  324.       int local_len = local_prompt ? strlen (local_prompt) : 0;
  325.       if (local_prompt_prefix && forced_display)
  326.     _rl_output_some_chars (local_prompt_prefix, strlen (local_prompt_prefix));
  327.  
  328.       if (local_len > 0)
  329.     strncpy (line + out, local_prompt, local_len);
  330.       out += local_len;
  331.       line[out] = '\0';
  332.       wrap_offset = local_len - visible_length;
  333.     }
  334.   else
  335.     {
  336.       int pmtlen;
  337.       prompt_this_line = strrchr (rl_display_prompt, '\n');
  338.       if (!prompt_this_line)
  339.     prompt_this_line = rl_display_prompt;
  340.       else
  341.     {
  342.       prompt_this_line++;
  343.       if (forced_display)
  344.         _rl_output_some_chars (rl_display_prompt, prompt_this_line - rl_display_prompt);
  345.     }
  346.  
  347.       pmtlen = strlen (prompt_this_line);
  348.       strncpy (line + out,  prompt_this_line, pmtlen);
  349.       out += pmtlen;
  350.       line[out] = '\0';
  351.       wrap_offset = 0;
  352.     }
  353.  
  354.   for (in = 0; in < rl_end; in++)
  355.     {
  356.       c = (unsigned char)rl_line_buffer[in];
  357.  
  358.       if (out + 8 >= line_size)        /* XXX - 8 for \t */
  359.     {
  360.       line_size *= 2;
  361.       visible_line = xrealloc (visible_line, line_size);
  362.       invisible_line = xrealloc (invisible_line, line_size);
  363.       line = invisible_line;
  364.     }
  365.  
  366.       if (in == rl_point)
  367.     c_pos = out;
  368.  
  369.       if (META_CHAR (c))
  370.     {
  371.       if (_rl_output_meta_chars == 0)
  372.         {
  373.           sprintf (line + out, "\\%o", c);
  374.           out += 4;
  375.         }
  376.       else
  377.         line[out++] = c;      
  378.     }
  379. #if defined (DISPLAY_TABS)
  380.       else if (c == '\t')
  381.     {
  382.       register int newout = (out | (int)7) + 1;
  383.       while (out < newout)
  384.         line[out++] = ' ';
  385.     }
  386. #endif
  387.       else if (c < ' ')
  388.     {
  389.       line[out++] = '^';
  390.       line[out++] = UNCTRL (c);    /* XXX was c ^ 0x40 */
  391.     }
  392.       else if (c == 127)
  393.     {
  394.       line[out++] = '^';
  395.       line[out++] = '?';
  396.     }
  397.       else
  398.     line[out++] = c;
  399.     }
  400.   line[out] = '\0';
  401.   if (c_pos < 0)
  402.     c_pos = out;
  403.  
  404.   /* C_POS == position in buffer where cursor should be placed. */
  405.  
  406.   /* PWP: now is when things get a bit hairy.  The visible and invisible
  407.      line buffers are really multiple lines, which would wrap every
  408.      (screenwidth - 1) characters.  Go through each in turn, finding
  409.      the changed region and updating it.  The line order is top to bottom. */
  410.  
  411.   /* If we can move the cursor up and down, then use multiple lines,
  412.      otherwise, let long lines display in a single terminal line, and
  413.      horizontally scroll it. */
  414.  
  415.   if (!_rl_horizontal_scroll_mode && term_up && *term_up)
  416.     {
  417.       int total_screen_chars = screenchars;
  418.       int nleft, cursor_linenum, pos, changed_screen_line;
  419.  
  420.       if (!rl_display_fixed || forced_display)
  421.     {
  422.       forced_display = 0;
  423.  
  424.       /* If we have more than a screenful of material to display, then
  425.          only display a screenful.  We should display the last screen,
  426.          not the first.  I'll fix this in a minute. */
  427.       if (out >= total_screen_chars)
  428.         out = total_screen_chars - 1;
  429.  
  430.       /* Number of screen lines to display.  The first line wraps at
  431.          (screenwidth + wrap_offset) chars, the rest of the lines have
  432.          screenwidth chars. */
  433.       nleft = out - wrap_offset + term_xn - 1;
  434.       inv_botlin = (nleft > 0) ? nleft / screenwidth : 0;
  435.  
  436.       /* The first line is at character position 0 in the buffer.  The
  437.          second and subsequent lines start at N * screenwidth, offset by
  438.          OFFSET.  OFFSET is wrap_offset for the invisible line and
  439.          visible_wrap_offset for the line currently displayed. */
  440.  
  441. #define W_OFFSET(line, offset) ((line) == 0 ? offset : 0)
  442. #define L_OFFSET(n, offset) ((n) > 0 ? ((n) * screenwidth) + (offset) : 0)
  443. #define VIS_CHARS(line) &visible_line[L_OFFSET((line), visible_wrap_offset)]
  444. #define VIS_LINE(line) ((line) > _rl_vis_botlin) ? "" : VIS_CHARS(line)
  445. #define INV_LINE(line) &invisible_line[L_OFFSET((line), wrap_offset)]
  446.  
  447.       /* For each line in the buffer, do the updating display. */
  448.       for (linenum = 0; linenum <= inv_botlin; linenum++)
  449.         {
  450.           update_line (VIS_LINE(linenum), INV_LINE(linenum), linenum,
  451.                screenwidth + W_OFFSET(linenum, visible_wrap_offset),
  452.                screenwidth + W_OFFSET(linenum, wrap_offset),
  453.                inv_botlin);
  454.  
  455.           /* If this is the line with the prompt, we might need to
  456.          compensate for invisible characters in the new line. Do
  457.          this only if there is not more than one new line (which
  458.          implies that we completely overwrite the old visible line)
  459.          and the new line is shorter than the old. */
  460.           if (linenum == 0 &&
  461.           inv_botlin == 0 &&
  462.           (wrap_offset > visible_wrap_offset) &&
  463.           (_rl_last_c_pos < visible_first_line_len))
  464.         {
  465.           nleft = screenwidth + wrap_offset - _rl_last_c_pos;
  466.           clear_to_eol (nleft);
  467.         }
  468.  
  469.           /* Since the new first line is now visible, save its length. */
  470.           if (linenum == 0)
  471.         visible_first_line_len = (inv_botlin > 0) ? screenwidth : out - wrap_offset;
  472.         }
  473.  
  474.       /* We may have deleted some lines.  If so, clear the left over
  475.          blank ones at the bottom out. */
  476.       if (_rl_vis_botlin > inv_botlin)
  477.         {
  478.           char *tt;
  479.           for (; linenum <= _rl_vis_botlin; linenum++)
  480.         {
  481.           tt = VIS_CHARS (linenum);
  482.           _rl_move_vert (linenum);
  483.           _rl_move_cursor_relative (0, tt);
  484.           clear_to_eol
  485.             ((linenum == _rl_vis_botlin) ? strlen (tt) : screenwidth);
  486.         }
  487.         }
  488.       _rl_vis_botlin = inv_botlin;
  489.  
  490.       /* Move the cursor where it should be. */
  491.       /* Which line? */
  492.       nleft = c_pos - wrap_offset - term_xn + 1;
  493.       cursor_linenum = (nleft > 0) ? nleft / screenwidth : 0;
  494.  
  495.       /* CHANGED_SCREEN_LINE is set to 1 if we have moved to a
  496.          different screen line during this redisplay. */
  497.       changed_screen_line = _rl_last_v_pos != cursor_linenum;
  498.       if (changed_screen_line)
  499.         {
  500.           _rl_move_vert (cursor_linenum);
  501.           /* If we moved up to the line with the prompt using term_up,
  502.              the physical cursor position on the screen stays the same,
  503.              but the buffer position needs to be adjusted to account
  504.              for invisible characters. */
  505.           if (cursor_linenum == 0 && wrap_offset)
  506.             _rl_last_c_pos += wrap_offset;
  507.         }
  508.  
  509.       /* We have to reprint the prompt if it contains invisible
  510.          characters, since it's not generally OK to just reprint
  511.          the characters from the current cursor position. */
  512.       nleft = visible_length + wrap_offset;
  513.       if (cursor_linenum == 0 && wrap_offset > 0 && _rl_last_c_pos > 0 &&
  514.           _rl_last_c_pos <= nleft && local_prompt)
  515.         {
  516.           if (term_cr)
  517.         tputs (term_cr, 1, _rl_output_character_function);
  518.           _rl_output_some_chars (local_prompt, nleft);
  519.           _rl_last_c_pos = nleft;
  520.         }
  521.  
  522.       /* Where on that line?  And where does that line start
  523.          in the buffer? */
  524.       pos = L_OFFSET(cursor_linenum, wrap_offset);
  525.       /* nleft == number of characters in the line buffer between the
  526.          start of the line and the cursor position. */
  527.       nleft = c_pos - pos;
  528.  
  529.       /* Since rl_backspace() doesn't know about invisible characters in the
  530.          prompt, and there's no good way to tell it, we compensate for
  531.          those characters here and call rl_backspace() directly. */
  532.       if (wrap_offset && cursor_linenum == 0 && nleft < _rl_last_c_pos)
  533.         {
  534.           rl_backspace (_rl_last_c_pos - nleft);
  535.           _rl_last_c_pos = nleft;
  536.         }
  537.  
  538.       if (nleft != _rl_last_c_pos)
  539.         _rl_move_cursor_relative (nleft, &invisible_line[pos]);
  540.     }
  541.     }
  542.   else                /* Do horizontal scrolling. */
  543.     {
  544. #define M_OFFSET(margin, offset) ((margin) == 0 ? offset : 0)
  545.       int lmargin, ndisp, nleft, phys_c_pos, t;
  546.  
  547.       /* Always at top line. */
  548.       _rl_last_v_pos = 0;
  549.  
  550.       /* Compute where in the buffer the displayed line should start.  This
  551.      will be LMARGIN. */
  552.  
  553.       /* The number of characters that will be displayed before the cursor. */
  554.       ndisp = c_pos - wrap_offset;
  555.       nleft  = visible_length + wrap_offset;
  556.       /* Where the new cursor position will be on the screen.  This can be
  557.          longer than SCREENWIDTH; if it is, lmargin will be adjusted. */
  558.       phys_c_pos = c_pos - (last_lmargin ? last_lmargin : wrap_offset);
  559.       t = screenwidth / 3;
  560.  
  561.       /* If the number of characters had already exceeded the screenwidth,
  562.          last_lmargin will be > 0. */
  563.  
  564.       /* If the number of characters to be displayed is more than the screen
  565.          width, compute the starting offset so that the cursor is about
  566.          two-thirds of the way across the screen. */
  567.       if (phys_c_pos > screenwidth - 2)
  568.     {
  569.       lmargin = c_pos - (2 * t);
  570.       if (lmargin < 0)
  571.         lmargin = 0;
  572.       /* If the left margin would be in the middle of a prompt with
  573.          invisible characters, don't display the prompt at all. */
  574.       if (wrap_offset && lmargin > 0 && lmargin < nleft)
  575.         lmargin = nleft;
  576.     }
  577.       else if (ndisp < screenwidth - 2)        /* XXX - was -1 */
  578.         lmargin = 0;
  579.       else if (phys_c_pos < 1)
  580.     {
  581.       /* If we are moving back towards the beginning of the line and
  582.          the last margin is no longer correct, compute a new one. */
  583.       lmargin = ((c_pos - 1) / t) * t;    /* XXX */
  584.       if (wrap_offset && lmargin > 0 && lmargin < nleft)
  585.         lmargin = nleft;
  586.     }
  587.       else
  588.         lmargin = last_lmargin;
  589.  
  590.       /* If the first character on the screen isn't the first character
  591.      in the display line, indicate this with a special character. */
  592.       if (lmargin > 0)
  593.     line[lmargin] = '<';
  594.  
  595.       /* If SCREENWIDTH characters starting at LMARGIN do not encompass
  596.          the whole line, indicate that with a special characters at the
  597.          right edge of the screen.  If LMARGIN is 0, we need to take the
  598.          wrap offset into account. */
  599.       t = lmargin + M_OFFSET (lmargin, wrap_offset) + screenwidth;
  600.       if (t < out)
  601.         line[t - 1] = '>';
  602.  
  603.       if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  604.     {
  605.       forced_display = 0;
  606.       update_line (&visible_line[last_lmargin],
  607.                &invisible_line[lmargin],
  608.                0,
  609.                screenwidth + visible_wrap_offset,
  610.                screenwidth + (lmargin ? 0 : wrap_offset),
  611.                0);
  612.  
  613.       /* If the visible new line is shorter than the old, but the number
  614.          of invisible characters is greater, and we are at the end of
  615.          the new line, we need to clear to eol. */
  616.       t = _rl_last_c_pos - M_OFFSET (lmargin, wrap_offset);
  617.       if ((M_OFFSET (lmargin, wrap_offset) > visible_wrap_offset) &&
  618.           (_rl_last_c_pos == out) &&
  619.           t < visible_first_line_len)
  620.         {
  621.           nleft = screenwidth - t;
  622.           clear_to_eol (nleft);
  623.         }
  624.       visible_first_line_len = out - lmargin - M_OFFSET (lmargin, wrap_offset);
  625.       if (visible_first_line_len > screenwidth)
  626.         visible_first_line_len = screenwidth;
  627.  
  628.       _rl_move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  629.       last_lmargin = lmargin;
  630.     }
  631.     }
  632.   fflush (rl_outstream);
  633.  
  634.   /* Swap visible and non-visible lines. */
  635.   {
  636.     char *temp = visible_line;
  637.     visible_line = invisible_line;
  638.     invisible_line = temp;
  639.     rl_display_fixed = 0;
  640.     /* If we are displaying on a single line, and last_lmargin is > 0, we
  641.        are not displaying any invisible characters, so set visible_wrap_offset
  642.        to 0. */
  643.     if (_rl_horizontal_scroll_mode && last_lmargin)
  644.       visible_wrap_offset = 0;
  645.     else
  646.       visible_wrap_offset = wrap_offset;
  647.   }
  648. }
  649.  
  650. /* PWP: update_line() is based on finding the middle difference of each
  651.    line on the screen; vis:
  652.  
  653.                  /old first difference
  654.     /beginning of line   |          /old last same       /old EOL
  655.     v             v          v            v
  656. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  657. new:    eddie> Oh, my little buggy says to me, as lurgid as
  658.     ^             ^    ^               ^
  659.     \beginning of line   |    \new last same       \new end of line
  660.                  \new first difference
  661.  
  662.    All are character pointers for the sake of speed.  Special cases for
  663.    no differences, as well as for end of line additions must be handeled.
  664.  
  665.    Could be made even smarter, but this works well enough */
  666. static void
  667. update_line (old, new, current_line, omax, nmax, inv_botlin)
  668.      register char *old, *new;
  669.      int current_line, omax, nmax;
  670. {
  671.   register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  672.   int temp, lendiff, wsatend, od, nd;
  673.  
  674.   /* If we're at the right edge of a terminal that supports xn, we're
  675.      ready to wrap around, so do so.  This fixes problems with knowing
  676.      the exact cursor position and cut-and-paste with certain terminal
  677.      emulators.  In this calculation, TEMP is the physical screen
  678.      position of the cursor. */
  679.   temp = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
  680.   if (temp == screenwidth && term_xn && !_rl_horizontal_scroll_mode
  681.       && _rl_last_v_pos == current_line - 1)
  682.     {
  683.       if (new[0])
  684.     putc (new[0], rl_outstream);
  685.       else
  686.     putc (' ', rl_outstream);
  687.       _rl_last_c_pos = 1;        /* XXX */
  688.       _rl_last_v_pos++;
  689.       if (old[0])
  690.         old[0] = new[0];
  691.     }
  692.       
  693.   /* Find first difference. */
  694.   for (ofd = old, nfd = new;
  695.        (ofd - old < omax) && *ofd && (*ofd == *nfd);
  696.        ofd++, nfd++)
  697.     ;
  698.  
  699.   /* Move to the end of the screen line.  ND and OD are used to keep track
  700.      of the distance between ne and new and oe and old, respectively, to
  701.      move a subtraction out of each loop. */
  702.   for (od = ofd - old, oe = ofd; od < omax && *oe; oe++, od++);
  703.   for (nd = nfd - new, ne = nfd; nd < nmax && *ne; ne++, nd++);
  704.  
  705.   /* If no difference, continue to next line. */
  706.   if (ofd == oe && nfd == ne)
  707.     return;
  708.  
  709.   wsatend = 1;            /* flag for trailing whitespace */
  710.   ols = oe - 1;            /* find last same */
  711.   nls = ne - 1;
  712.   while ((ols > ofd) && (nls > nfd) && (*ols == *nls))
  713.     {
  714.       if (*ols != ' ')
  715.     wsatend = 0;
  716.       ols--;
  717.       nls--;
  718.     }
  719.  
  720.   if (wsatend)
  721.     {
  722.       ols = oe;
  723.       nls = ne;
  724.     }
  725.   else if (*ols != *nls)
  726.     {
  727.       if (*ols)            /* don't step past the NUL */
  728.     ols++;
  729.       if (*nls)
  730.     nls++;
  731.     }
  732.  
  733.   _rl_move_vert (current_line);
  734.  
  735.   /* If this is the first line and there are invisible characters in the
  736.      prompt string, and the prompt string has not changed, then redraw
  737.      the entire prompt string.  We can only do this reliably if the
  738.      terminal supports a `cr' capability.
  739.  
  740.      This is more than just an efficiency hack -- there is a problem with
  741.      redrawing portions of the prompt string if they contain terminal
  742.      escape sequences (like drawing the `unbold' sequence without a
  743.      corresponding `bold') that manifests itself on certain terminals. */
  744.  
  745.   lendiff = strlen (local_prompt);
  746.   if (current_line == 0 && !_rl_horizontal_scroll_mode &&
  747.       lendiff > visible_length &&
  748.       _rl_last_c_pos > 0 && (ofd - old) >= lendiff && term_cr)
  749.     {
  750.       tputs (term_cr, 1, _rl_output_character_function);
  751.       _rl_output_some_chars (local_prompt, lendiff);
  752.       _rl_last_c_pos = lendiff;
  753.     }
  754.  
  755.   _rl_move_cursor_relative (ofd - old, old);
  756.  
  757.   /* if (len (new) > len (old)) */
  758.   lendiff = (nls - nfd) - (ols - ofd);
  759.  
  760.   /* Insert (diff (len (old), len (new)) ch. */
  761.   temp = ne - nfd;
  762.   if (lendiff > 0)
  763.     {
  764.       /* Non-zero if we're increasing the number of lines. */
  765.       int gl = current_line >= _rl_vis_botlin && inv_botlin > _rl_vis_botlin;
  766.       /* Sometimes it is cheaper to print the characters rather than
  767.      use the terminal's capabilities.  If we're growing the number
  768.      of lines, make sure we actually cause the new line to wrap
  769.      around on auto-wrapping terminals. */
  770.       if (terminal_can_insert && ((2 * temp) >= lendiff || term_IC) && (!term_xn || !gl))
  771.     {
  772.       /* If lendiff > visible_length and _rl_last_c_pos == 0 and
  773.          _rl_horizontal_scroll_mode == 1, inserting the characters with
  774.          term_IC or term_ic will screw up the screen because of the
  775.          invisible characters.  We need to just draw them. */
  776.       if (*ols && (!_rl_horizontal_scroll_mode || _rl_last_c_pos > 0 ||
  777.             lendiff <= visible_length))
  778.         {
  779.           insert_some_chars (nfd, lendiff);
  780.           _rl_last_c_pos += lendiff;
  781.         }
  782.       else
  783.         {
  784.           /* At the end of a line the characters do not have to
  785.          be "inserted".  They can just be placed on the screen. */
  786.           _rl_output_some_chars (nfd, lendiff);
  787.           _rl_last_c_pos += lendiff;
  788.         }
  789.       /* Copy (new) chars to screen from first diff to last match. */
  790.       temp = nls - nfd;
  791.       if ((temp - lendiff) > 0)
  792.         {
  793.           _rl_output_some_chars (nfd + lendiff, temp - lendiff);
  794.           _rl_last_c_pos += temp - lendiff;
  795.         }
  796.     }
  797.       else
  798.     {
  799.       /* cannot insert chars, write to EOL */
  800.       _rl_output_some_chars (nfd, temp);
  801.       _rl_last_c_pos += temp;
  802.     }
  803.     }
  804.   else                /* Delete characters from line. */
  805.     {
  806.       /* If possible and inexpensive to use terminal deletion, then do so. */
  807.       if (term_dc && (2 * temp) >= -lendiff)
  808.     {
  809.       /* If all we're doing is erasing the invisible characters in the
  810.          prompt string, don't bother.  It screws up the assumptions
  811.          about what's on the screen. */
  812.       if (_rl_horizontal_scroll_mode && _rl_last_c_pos == 0 &&
  813.           -lendiff == visible_wrap_offset)
  814.         lendiff = 0;
  815.  
  816.       if (lendiff)
  817.         delete_chars (-lendiff); /* delete (diff) characters */
  818.  
  819.       /* Copy (new) chars to screen from first diff to last match */
  820.       temp = nls - nfd;
  821.       if (temp > 0)
  822.         {
  823.           _rl_output_some_chars (nfd, temp);
  824.           _rl_last_c_pos += temp;
  825.         }
  826.     }
  827.       /* Otherwise, print over the existing material. */
  828.       else
  829.     {
  830.       if (temp > 0)
  831.         {
  832.           _rl_output_some_chars (nfd, temp);
  833.           _rl_last_c_pos += temp;
  834.         }
  835.       lendiff = (oe - old) - (ne - new);
  836.       if (term_xn && current_line < inv_botlin)
  837.         space_to_eol (lendiff);
  838.       else
  839.         clear_to_eol (lendiff);
  840.     }
  841.     }
  842. }
  843.  
  844. /* Tell the update routines that we have moved onto a new (empty) line. */
  845. rl_on_new_line ()
  846. {
  847.   if (visible_line)
  848.     visible_line[0] = '\0';
  849.  
  850.   _rl_last_c_pos = _rl_last_v_pos = 0;
  851.   _rl_vis_botlin = last_lmargin = 0;
  852.   return 0;
  853. }
  854.  
  855. /* Actually update the display, period. */
  856. rl_forced_update_display ()
  857. {
  858.   if (visible_line)
  859.     {
  860.       register char *temp = visible_line;
  861.  
  862.       while (*temp) *temp++ = '\0';
  863.     }
  864.   rl_on_new_line ();
  865.   forced_display++;
  866.   rl_redisplay ();
  867.   return 0;
  868. }
  869.  
  870. /* Move the cursor from _rl_last_c_pos to NEW, which are buffer indices.
  871.    DATA is the contents of the screen line of interest; i.e., where
  872.    the movement is being done. */
  873. void
  874. _rl_move_cursor_relative (new, data)
  875.      int new;
  876.      char *data;
  877. {
  878.   register int i;
  879.  
  880.   /* If we don't have to do anything, then return. */
  881.   if (_rl_last_c_pos == new) return;
  882.  
  883.   /* It may be faster to output a CR, and then move forwards instead
  884.      of moving backwards. */
  885.   /* i == current physical cursor position. */
  886.   i = _rl_last_c_pos - W_OFFSET(_rl_last_v_pos, visible_wrap_offset);
  887.   if (CR_FASTER (new, _rl_last_c_pos) || (term_xn && i == screenwidth))
  888.     {
  889. #if defined (__MSDOS__)
  890.       putc ('\r', rl_outstream);
  891. #else
  892.       tputs (term_cr, 1, _rl_output_character_function);
  893. #endif /* !__MSDOS__ */
  894.       _rl_last_c_pos = 0;
  895.     }
  896.  
  897.   if (_rl_last_c_pos < new)
  898.     {
  899.       /* Move the cursor forward.  We do it by printing the command
  900.      to move the cursor forward if there is one, else print that
  901.      portion of the output buffer again.  Which is cheaper? */
  902.  
  903.       /* The above comment is left here for posterity.  It is faster
  904.      to print one character (non-control) than to print a control
  905.      sequence telling the terminal to move forward one character.
  906.      That kind of control is for people who don't know what the
  907.      data is underneath the cursor. */
  908. #if defined (HACK_TERMCAP_MOTION)
  909.       extern char *term_forward_char;
  910.  
  911.       if (term_forward_char)
  912.     for (i = _rl_last_c_pos; i < new; i++)
  913.       tputs (term_forward_char, 1, _rl_output_character_function);
  914.       else
  915.     for (i = _rl_last_c_pos; i < new; i++)
  916.       putc (data[i], rl_outstream);
  917. #else
  918.       for (i = _rl_last_c_pos; i < new; i++)
  919.     putc (data[i], rl_outstream);
  920. #endif /* HACK_TERMCAP_MOTION */
  921.     }
  922.   else if (_rl_last_c_pos != new)
  923.     rl_backspace (_rl_last_c_pos - new);
  924.   _rl_last_c_pos = new;
  925. }
  926.  
  927. /* PWP: move the cursor up or down. */
  928. void
  929. _rl_move_vert (to)
  930.      int to;
  931. {
  932.   register int delta, i;
  933.  
  934.   if (_rl_last_v_pos == to || to > screenheight)
  935.     return;
  936.  
  937. #if defined (__GO32__)
  938.   {
  939.     int row, col;
  940.  
  941.     ScreenGetCursor (&row, &col);
  942.     ScreenSetCursor ((row + to - _rl_last_v_pos), col);
  943.   }
  944. #else /* !__GO32__ */
  945.  
  946.   if ((delta = to - _rl_last_v_pos) > 0)
  947.     {
  948.       for (i = 0; i < delta; i++)
  949.     putc ('\n', rl_outstream);
  950.       tputs (term_cr, 1, _rl_output_character_function);
  951.       _rl_last_c_pos = 0;
  952.     }
  953.   else
  954.     {            /* delta < 0 */
  955.       if (term_up && *term_up)
  956.     for (i = 0; i < -delta; i++)
  957.       tputs (term_up, 1, _rl_output_character_function);
  958.     }
  959. #endif /* !__GO32__ */
  960.   _rl_last_v_pos = to;        /* Now TO is here */
  961. }
  962.  
  963. /* Physically print C on rl_outstream.  This is for functions which know
  964.    how to optimize the display.  Return the number of characters output. */
  965. rl_show_char (c)
  966.      int c;
  967. {
  968.   int n = 1;
  969.   if (META_CHAR (c) && (_rl_output_meta_chars == 0))
  970.     {
  971.       fprintf (rl_outstream, "M-");
  972.       n += 2;
  973.       c = UNMETA (c);
  974.     }
  975.  
  976. #if defined (DISPLAY_TABS)
  977.   if (c < 32 && c != '\t')
  978. #else
  979.   if (c < 32)
  980. #endif /* !DISPLAY_TABS */
  981.     {
  982.       fprintf (rl_outstream, "C-");
  983.       n += 2;
  984.       c += 64;
  985.     }
  986.  
  987.   putc (c, rl_outstream);
  988.   fflush (rl_outstream);
  989.   return n;
  990. }
  991.  
  992. int
  993. rl_character_len (c, pos)
  994.      register int c, pos;
  995. {
  996.   unsigned char uc;
  997.  
  998.   uc = (unsigned char)c;
  999.  
  1000.   if (META_CHAR (uc))
  1001.     return ((_rl_output_meta_chars == 0) ? 4 : 1);
  1002.  
  1003.   if (uc == '\t')
  1004.     {
  1005. #if defined (DISPLAY_TABS)
  1006.       return (((pos | 7) + 1) - pos);
  1007. #else
  1008.       return (2);
  1009. #endif /* !DISPLAY_TABS */
  1010.     }
  1011.  
  1012.   return ((isprint (uc)) ? 1 : 2);
  1013. }
  1014.  
  1015. /* How to print things in the "echo-area".  The prompt is treated as a
  1016.    mini-modeline. */
  1017.  
  1018. #if defined (HAVE_VARARGS_H)
  1019. rl_message (va_alist)
  1020.      va_dcl
  1021. {
  1022.   char *format;
  1023.   va_list args;
  1024.  
  1025.   va_start (args);
  1026.   format = va_arg (args, char *);
  1027.   vsprintf (msg_buf, format, args);
  1028.   va_end (args);
  1029.  
  1030.   rl_display_prompt = msg_buf;
  1031.   rl_redisplay ();
  1032.   return 0;
  1033. }
  1034. #else /* !HAVE_VARARGS_H */
  1035. rl_message (format, arg1, arg2)
  1036.      char *format;
  1037. {
  1038.   sprintf (msg_buf, format, arg1, arg2);
  1039.   rl_display_prompt = msg_buf;
  1040.   rl_redisplay ();
  1041.   return 0;
  1042. }
  1043. #endif /* !HAVE_VARARGS_H */
  1044.  
  1045. /* How to clear things from the "echo-area". */
  1046. rl_clear_message ()
  1047. {
  1048.   rl_display_prompt = rl_prompt;
  1049.   rl_redisplay ();
  1050.   return 0;
  1051. }
  1052.  
  1053. rl_reset_line_state ()
  1054. {
  1055.   rl_on_new_line ();
  1056.  
  1057.   rl_display_prompt = rl_prompt ? rl_prompt : "";
  1058.   forced_display = 1;
  1059.   return 0;
  1060. }
  1061.  
  1062. /* Quick redisplay hack when erasing characters at the end of the line. */
  1063. void
  1064. _rl_erase_at_end_of_line (l)
  1065.      int l;
  1066. {
  1067.   register int i;
  1068.  
  1069.   rl_backspace (l);
  1070.   for (i = 0; i < l; i++)
  1071.     putc (' ', rl_outstream);
  1072.   rl_backspace (l);
  1073.   for (i = 0; i < l; i++)
  1074.     visible_line[--_rl_last_c_pos] = '\0';
  1075.   rl_display_fixed++;
  1076. }
  1077.  
  1078. /* Clear to the end of the line.  COUNT is the minimum
  1079.    number of character spaces to clear, */
  1080. static void
  1081. clear_to_eol (count)
  1082.      int count;
  1083. {
  1084. #if !defined (__GO32__)
  1085.   if (term_clreol)
  1086.     {
  1087.       tputs (term_clreol, 1, _rl_output_character_function);
  1088.     }
  1089.   else
  1090. #endif /* !__GO32__ */
  1091.     space_to_eol (count);
  1092. }
  1093.  
  1094. /* Clear to the end of the line using spaces.  COUNT is the minimum
  1095.    number of character spaces to clear, */
  1096. static void
  1097. space_to_eol (count)
  1098.      int count;
  1099. {
  1100.   register int i;
  1101.  
  1102.   for (i = 0; i < count; i++)
  1103.    putc (' ', rl_outstream);
  1104.  
  1105.   _rl_last_c_pos += count;
  1106. }
  1107.  
  1108. /* Insert COUNT characters from STRING to the output stream. */
  1109. static void
  1110. insert_some_chars (string, count)
  1111.      char *string;
  1112.      int count;
  1113. {
  1114. #if defined (__GO32__)
  1115.   int row, col, width;
  1116.   char *row_start;
  1117.  
  1118.   ScreenGetCursor (&row, &col);
  1119.   width = ScreenCols ();
  1120.   row_start = ScreenPrimary + (row * width);
  1121.  
  1122.   memcpy (row_start + col + count, row_start + col, width - col - count);
  1123.  
  1124.   /* Place the text on the screen. */
  1125.   _rl_output_some_chars (string, count);
  1126. #else /* !_GO32 */
  1127.  
  1128.   /* If IC is defined, then we do not have to "enter" insert mode. */
  1129.   if (term_IC)
  1130.     {
  1131.       char *tgoto (), *buffer;
  1132.       buffer = tgoto (term_IC, 0, count);
  1133.       tputs (buffer, 1, _rl_output_character_function);
  1134.       _rl_output_some_chars (string, count);
  1135.     }
  1136.   else
  1137.     {
  1138.       register int i;
  1139.  
  1140.       /* If we have to turn on insert-mode, then do so. */
  1141.       if (term_im && *term_im)
  1142.     tputs (term_im, 1, _rl_output_character_function);
  1143.  
  1144.       /* If there is a special command for inserting characters, then
  1145.      use that first to open up the space. */
  1146.       if (term_ic && *term_ic)
  1147.     {
  1148.       for (i = count; i--; )
  1149.         tputs (term_ic, 1, _rl_output_character_function);
  1150.     }
  1151.  
  1152.       /* Print the text. */
  1153.       _rl_output_some_chars (string, count);
  1154.  
  1155.       /* If there is a string to turn off insert mode, we had best use
  1156.      it now. */
  1157.       if (term_ei && *term_ei)
  1158.     tputs (term_ei, 1, _rl_output_character_function);
  1159.     }
  1160. #endif /* !__GO32__ */
  1161. }
  1162.  
  1163. /* Delete COUNT characters from the display line. */
  1164. static void
  1165. delete_chars (count)
  1166.      int count;
  1167. {
  1168. #if defined (__GO32__)
  1169.   int row, col, width;
  1170.   char *row_start;
  1171.  
  1172.   ScreenGetCursor (&row, &col);
  1173.   width = ScreenCols ();
  1174.   row_start = ScreenPrimary + (row * width);
  1175.  
  1176.   memcpy (row_start + col, row_start + col + count, width - col - count);
  1177.   memset (row_start + width - count, 0, count * 2);
  1178. #else /* !_GO32 */
  1179.  
  1180.   if (count > screenwidth)    /* XXX */
  1181.     return;
  1182.  
  1183.   if (term_DC && *term_DC)
  1184.     {
  1185.       char *tgoto (), *buffer;
  1186.       buffer = tgoto (term_DC, count, count);
  1187.       tputs (buffer, count, _rl_output_character_function);
  1188.     }
  1189.   else
  1190.     {
  1191.       if (term_dc && *term_dc)
  1192.     while (count--)
  1193.       tputs (term_dc, 1, _rl_output_character_function);
  1194.     }
  1195. #endif /* !__GO32__ */
  1196. }
  1197.  
  1198. void
  1199. _rl_update_final ()
  1200. {
  1201.   int full_lines;
  1202.  
  1203.   full_lines = 0;
  1204.   if (_rl_vis_botlin && visible_line[screenwidth * _rl_vis_botlin] == 0)
  1205.     {
  1206.       _rl_vis_botlin--;
  1207.       full_lines = 1;
  1208.     }
  1209.   _rl_move_vert (_rl_vis_botlin);
  1210.   if (full_lines && term_xn)
  1211.     {
  1212.       /* Remove final line-wrap flag in xterm. */
  1213.       char *last_line;
  1214.       last_line = &visible_line[screenwidth * _rl_vis_botlin];
  1215.       _rl_move_cursor_relative (screenwidth - 1, last_line);
  1216.       clear_to_eol (0);
  1217.       putc (last_line[screenwidth - 1], rl_outstream);
  1218.     }
  1219.   _rl_vis_botlin = 0;
  1220.   crlf ();
  1221.   fflush (rl_outstream);
  1222.   rl_display_fixed++;
  1223. }
  1224.